﻿using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Lib.Action.Terran
{
	public class ActionProduceRefinery : ActionProduceFromSCV
	{
		public static ActionProduceRefinery Instance = new ActionProduceRefinery();

		private ActionProduceRefinery() : base(30, 75, 0) { }

		public override string EventName { get { return "ActionSpawnRefinery"; } }

		public override bool HaveResources(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return base.HaveResources(bo) && state.Refinery.Total + 1 <= bo.State.Bases.Total * 2;
		}

		public override bool HaveTechnology(IBuildOrder bo) { return true; }

		public override bool IsInvalid(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return state.Refinery.Total + 1 > bo.State.Bases.Total * 2;
		}

		public override TerranState.TerranBuilding GetBuilding(TerranState state) { return state.Refinery; }

		public override void Execute(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			if (state.WorkersOnMinerals > 0)
			{
				state.WorkersOnMinerals--;
				bo.State.Minerals -= Minerals;
				OnProductionStart(bo);
				bo.AddAction(state.Time + BuildTime, new ActionGeneric(EventName, delegate(IBuildOrder ibo)
				{
					state.WorkersOnGas++;
					OnProductionFinished(ibo);
				}));
			}
			else if (state.WorkersOnGas > 0)
			{
				state.WorkersOnGas--;
				bo.State.Minerals -= Minerals;
				OnProductionStart(bo);
				bo.AddAction(state.Time + BuildTime, new ActionGeneric(EventName, delegate(IBuildOrder ibo)
				{
					state.WorkersOnGas++;
					OnProductionFinished(ibo);
				}));
			}
			else throw new InvalidActionException();
		}
	}
}
